-- Renombrar secuencia para que cumpla con standard de nombres
-- Ale - 31/05/2019

ALTER SEQUENCE subsistemas.sub_balanza_turnotara_sec_next_id RENAME TO seq_sub_balanza_turnotara;



-- Function: subsistemas.tg_fechas_sub_balanza_turnotara()

-- DROP FUNCTION subsistemas.tg_fechas_sub_balanza_turnotara();

CREATE OR REPLACE FUNCTION subsistemas.tg_fechas_sub_balanza_turnotara()
  RETURNS trigger AS
$BODY$
BEGIN
  IF (TG_OP = 'INSERT') THEN
     NEW.Id := (SELECT NEXTVAL('subsistemas.seq_sub_balanza_turnotara'));
     NEW.FECHAINSERT := NOW(); 
     IF NEW.TARA IS NOT NULL THEN 
        NEW.FECHATARA := NOW();
     END IF;   
  END IF;
  IF (TG_OP = 'UPDATE') THEN
     IF NEW.TARA IS NOT NULL AND OLD.TARA IS NULL THEN 
        NEW.FECHATARA := NOW();
     END IF;
     IF NEW.BRUTO IS NOT NULL AND OLD.BRUTO IS NULL THEN 
        NEW.FECHABRUTO := NOW();
     END IF;
  END IF;

RETURN NEW;
END;
$BODY$
  LANGUAGE plpgsql VOLATILE COST 100;
ALTER FUNCTION subsistemas.tg_fechas_sub_balanza_turnotara() OWNER TO abacouser;


-- =============================
SELECT aaversion('407.04');
-- =============================